<template>
    <div class="product-card">
      <div class="product-item"  @click="onClickProduct">
        <el-image  :src="item.imgPath" :fit="fit" class="product-image" />
        <div class="product-info">
          <p class="name">{{ item.name }}</p>
          <p class="subtitle" :title="item.info"><span class="brand">自营</span>{{ellipsis( item.info,20) }}</p>
          <div class="price-info">
            <span class="price">曼波币 {{ item.discountPrice }}</span>
            <span class="stockNum">库存剩余{{ item.num }}</span>
          </div>
          
        </div>
      </div>
      <div class="action-buttons">
        <el-button type="primary" size="small" plain class="buy-button" :disabled="item.num<=0||!item.onSale"  @click="onClickPay" >立即购买</el-button>
       
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { defineProps, defineEmits } from 'vue'
  import {ellipsis} from '@/utils/util'
    // 定义商品接口，更新字段以匹配后端模型
interface ProductItem {
  id: number;              // 映射到后端的id字段
  name: string;            // 商品名称
  info: string;            // 商品简介
  price: string;           // 商品价格
  discountPrice?: string;  // 折扣价，可能为空
  imgPath: string;         // 商品图片路径
  num: number;             // 库存数量
  limitNum: number;        // 单次最大购买数量
  onSale: boolean;         // 是否在售
  bossId: number;          // 商家ID
  bossName: string;        // 商家名称
  bossAvatar: string;      // 商家头像
}
  // 接收商品数据作为 props
  const props = defineProps<{
  item: ProductItem;  // 使用 ProductItem 作为 item 的类型
}>();
  
  // 发出点击事件
  const emits = defineEmits(['pay', 'showProduct'])
  const onClickProduct = () => {
    emits('showProduct', props.item)
  }
  const onClickPay = () => {
    emits('pay', props.item)
  }
  </script>
  
  <style scoped>
  .product-card {
    width: 230px;
    max-height: 400px;
    border-radius: 8px;
    padding: 0;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  }
  .product-card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  }
  .product-item {
    display: flex;
    flex-direction: column;
    align-items: center;

    cursor: pointer;
  }
  
  .product-image {
    margin: top 5px; 
    width: 230px;
    height: 230px;
    margin: 0;
  }
  
  .product-info {
    width: 100%;
    text-align: center;
  }
  
  .name {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
  }
  
  .subtitle {
    font-size: 14px;
    color: #666;
    margin-bottom: 8px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
  
  .price-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    margin-top: 8px;
  }
  .brand{
    color: #999;
    margin-right: 10px;
  }
  .price {
    color: #ff4d4f;
    font-size: 18px;
    font-weight: bold;
  }
  
  .stockNum {
    color: #999;
  }
  
  .action-buttons {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-top: 16px;
    padding-bottom:5px ;
  }
  
   .buy-button {
     width: 90%;
     margin: auto;
   }
  </style>
  